package com.zlsy.springbootdemo.memory;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouliang
 * @date 2021/01/20
 */
public class HeapTest {
  private byte[] bs = new byte[2*1024 * 1024];

  /**
   * JDK8测试GC日志输出初始化堆大小、最大堆大小、初始化新生代大小，最大新生代大小设置：
   *
   * <p>``` -XX:+UseConcMarkSweepGC -Xms10m -Xmx10m -XX:NewSize=3m -XX:MaxNewSize=3m -XX:+PrintGC
   * -XX:+PrintGCDetails -XX:+PrintHeapAtGC -XX:+PrintGCTimeStamps ```
   */
  public static void main(String[] args) {
    List<HeapTest> list = new ArrayList<>();
    int num = 0;
    try {
      while (true) {
        list.add(new HeapTest());
        num++;
      }
    } catch (Throwable e) {
      System.err.println("now error , num  == " + num);
      e.printStackTrace();
    }

    System.out.println("totalMemory:" + Runtime.getRuntime().totalMemory() / 1024.0 / 1024);
    System.out.println("freeMemory:" + Runtime.getRuntime().freeMemory() / 1024.0 / 1024);
    System.out.println("maxMemory:" + Runtime.getRuntime().maxMemory() / 1024.0 / 1024);
  }
}
